﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Application.BOM;
using FriendStar.Application.MainData;
using FriendStar.Domain.Entity.MainData;

namespace FriendStar.Web.Areas.MainData.Controllers
{
    public class ContainerController : ControllerBase
    {
        //
        // GET: /MainData/Container/
        V_BUS_SJ_DictionaryApp v_bus_sj_dictionaryApp = new V_BUS_SJ_DictionaryApp();
        ZSJ_B1_GH_SupplierInformationApp zsj_b1_gh_supplierinformationApp = new ZSJ_B1_GH_SupplierInformationApp();
        V_ZSJ_21_RQ_DatContainerApp v_zsj_21_rq_datcontainerApp = new V_ZSJ_21_RQ_DatContainerApp();
        ZSJ_21_RQ_DatContainerApp zsj_21_rq_datcontainerApp = new Application.MainData.ZSJ_21_RQ_DatContainerApp();
        ZSJ_22_TP_PalletApp zsj_22_tp_palletApp = new ZSJ_22_TP_PalletApp();
        V_ZSJ_23_GH_SuContainerApp v_zsj_23_gh_sucontainerApp = new V_ZSJ_23_GH_SuContainerApp();
        ZSJ_23_GH_SuContainerApp zsj_23_gh_sucontainerApp = new ZSJ_23_GH_SuContainerApp();
        V_ZSJ_23_GH_SuContainerNApp v_zsj_23_gh_sucontainernApp = new V_ZSJ_23_GH_SuContainerNApp();

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult ContainerList()
        {
            ViewBag.containerType = v_bus_sj_dictionaryApp.GetListByClassNo("02");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetContainerListJosn(Pagination pagination, string FContainer, string FContainerTypeNO)
        {
            var data = new
            {
                rows = v_zsj_21_rq_datcontainerApp.GetList(pagination, FContainer, FContainerTypeNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetContainerSortJson(string sidx, string sord, int rows, string FContainer, string FContainerTypeNO, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_zsj_21_rq_datcontainerApp.GetList(pagination, FContainer, FContainerTypeNO);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].FContainerID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult ContainerListInput(string status, string number)
        {
            ViewBag.status = (status != "U") ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.containerType = v_bus_sj_dictionaryApp.GetListByClassNo("02");
            if (status == "U")
            {
                ViewBag.oldEntity = v_zsj_21_rq_datcontainerApp.GetItem(number).ToJson();
            }
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitContainerListForm(string status, ZSJ_21_RQ_DatContainerEntity entity, string keyValue)
        {
            if (status == "I")
            {
                if (zsj_21_rq_datcontainerApp.CheckedEntity(entity))
                {
                    zsj_21_rq_datcontainerApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.FContainerID);
                }
                else
                {
                    return Error("新增失败，已经存在相同容器编号数据。");
                }
            }
            else if (status == "U")
            {
                if (zsj_21_rq_datcontainerApp.CheckedEntity(entity))
                {
                    ZSJ_21_RQ_DatContainerEntity oldEntity = zsj_21_rq_datcontainerApp.GetForm(entity.FContainerID);
                    if (oldEntity != null)
                    {
                        zsj_21_rq_datcontainerApp.SubmitForm(entity, entity.FContainerID);

                        return Success("修改操作成功。");
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的容器数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同容器编号数据。");
                }
            }
            else if (status == "D")
            {

                ZSJ_21_RQ_DatContainerEntity oldEntity = zsj_21_rq_datcontainerApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    zsj_21_rq_datcontainerApp.DeleteForm(oldEntity.FContainerID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的容器数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult PalletList()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetPalletListJosn(Pagination pagination, string Pallet)
        {
            var data = new
            {
                rows = zsj_22_tp_palletApp.GetList(pagination, Pallet),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetPalletSortJson(string sidx, string sord, int rows, string Pallet, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = zsj_22_tp_palletApp.GetList(pagination, Pallet);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].PalletID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitPalletListForm(string status, ZSJ_22_TP_PalletEntity entity, string keyValue)
        {
            if (status == "I")
            {
                if (zsj_22_tp_palletApp.CheckedEntity(entity))
                {
                    zsj_22_tp_palletApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.PalletID);
                }
                else
                {
                    return Error("新增失败，已经存在相同托盘编号数据。");
                }
            }
            else if (status == "U")
            {
                if (zsj_22_tp_palletApp.CheckedEntity(entity))
                {
                    ZSJ_22_TP_PalletEntity oldEntity = zsj_22_tp_palletApp.GetForm(entity.PalletID);
                    if (oldEntity != null)
                    {
                        zsj_22_tp_palletApp.SubmitForm(entity, entity.PalletID);
                        return Success("修改操作成功。");
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的托盘数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同托盘编号数据。");
                }
            }
            else if (status == "D")
            {

                ZSJ_22_TP_PalletEntity oldEntity = zsj_22_tp_palletApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    zsj_22_tp_palletApp.DeleteForm(oldEntity.PalletID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的托盘数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult SupplierList()
        {
            ViewBag.containerType = v_bus_sj_dictionaryApp.GetListByClassNo("02");
            ViewBag.proCompany = zsj_b1_gh_supplierinformationApp.GetList("IsProduct ='1'");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSupplierListJson(Pagination pagination, string FContainer, string FProCompanyNO, string FContainerTypeNO)
        {
            var data = new
            {
                rows = v_zsj_23_gh_sucontainerApp.GetList(pagination, FContainer, FProCompanyNO, FContainerTypeNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSupplierSortJson(string sidx, string sord, int rows, string FContainer, string FProCompanyNO, string FContainerTypeNO, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_zsj_23_gh_sucontainerApp.GetList(pagination, FContainer, FProCompanyNO, FContainerTypeNO);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].FContainerID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult GetSupplierNListJson(Pagination pagination, string FContainer)
        {
            var data = new
            {
                rows = v_zsj_23_gh_sucontainernApp.GetList(pagination, FContainer),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult SupplierListInput(string status, string number, string type)
        {
            ViewBag.status = (status != "U") ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.type = (type != "1") ? "0" : type;
            ViewBag.containerType = v_bus_sj_dictionaryApp.GetListByClassNo("02");
            ViewBag.proCompany = zsj_b1_gh_supplierinformationApp.GetList("IsProduct ='1'");
            ViewBag.oldEntity = v_zsj_23_gh_sucontainerApp.GetForm(number).ToJson();
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitSupplierListForm(string status, ZSJ_23_GH_SuContainerEntity entity, string keyValue)
        {
            if (status == "I")
            {
                entity.FContainerID = "";
                if (zsj_23_gh_sucontainerApp.CheckedEntity(entity))
                {
                    zsj_23_gh_sucontainerApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.FContainerID);
                }
                else
                {
                    return Error("新增失败，已经存在相同供应商和容器编号数据。");
                }
            }
            else if (status == "U")
            {
                if (zsj_23_gh_sucontainerApp.CheckedEntity(entity))
                {
                    ZSJ_23_GH_SuContainerEntity oldEntity = zsj_23_gh_sucontainerApp.GetForm(entity.FContainerID);
                    if (oldEntity != null)
                    {
                        zsj_23_gh_sucontainerApp.SubmitForm(entity, entity.FContainerID);
                        return Success("修改操作成功。", entity.FContainerID);
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的供应商和容器数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同供应商和容器数据。");
                }
            }
            else if (status == "D")
            {

                ZSJ_23_GH_SuContainerEntity oldEntity = zsj_23_gh_sucontainerApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    zsj_23_gh_sucontainerApp.DeleteForm(oldEntity.FContainerID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的供应商和容器数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }
    }
}
